#pragma once
#include "GraphicsDeviceContext.h"

namespace XamlFx
{
    [Windows::Foundation::Metadata::WebHostHidden]
    public ref class Effect : public xaml::DependencyObject
    {
    public:
        virtual ~Effect();
    internal:
        Effect();
        CPPCX_AUTO_PROPERTY(Padding, xaml::Thickness)
    protected private:
        virtual auto GetEffect    (d2d::Bitmap const&) -> d2d::Effect { return nullptr; }
        virtual void OnInitialized(){}
        void         Invalidate   ();
        auto         GetLogicalDpi() -> float;
    internal:
        auto ApplyEffectAsync   (dxgi::Surface const& renderTargetSurface, Dx::Point2F offset) -> concurrency::task<void>;
        void SetGraphicsContext (GraphicsDeviceContextPtr const& gfx);
        void SetUIElement       (xaml::UIElement^ element);
        auto CreateEffect       (IID const& effectId) -> d2d::Effect;
        auto RenderAsync        () -> concurrency::task<Dx::SizeU>;
        bool IsValid            ();
        void ResetResources     ();
        auto GetElementPixelSize() -> Dx::SizeU;
    private:
        void OnDpiChanged       (display::DisplayInformation ^sender, Platform::Object ^args);
        void EnsureSourceBitmap ();
    private:
        float                             m_logicalDpi;
        xamlImaging::RenderTargetBitmap^  m_renderTargetBitmap;
        GraphicsDeviceContextPtr          m_gfx;
        d2d::Bitmap                       m_sourceBitmap;
        std::atomic<bool>                 m_isRendering;
        xaml::UIElement^                  m_element;
        Dx::SizeU                         m_lastSize;
        Dx::SizeU                         m_rtbSize;
        bool                              m_isValid;
    };
}
